package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import domain.Usuario;

public class JDBCLogin implements ILogin {

	
	private Connection conn;
	
	public JDBCLogin(){
		conn = ConnectionManager.getInstance().checkOut();
	}
	
	public boolean singIn(String user, String pass) {
		 boolean res=false;
		 String sql = "SELECT * FROM user WHERE (nick = ? ) ";
	        PreparedStatement stmt = null;
	        ResultSet result = null;
	       

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, user);

	            result = stmt.executeQuery();

	            result.next();
	            if(user.equals(result.getString("nick")) && pass.equals(result.getString("pass"))){
	            	res=true;
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }	
	            } catch (SQLException e) {
	            }
	        }

	        return res;

	}
	
	public Integer obtenerOID(String user, String pass) {
		 Integer res=0;
		 String sql = "SELECT * FROM user WHERE (nick = ?  && pass=?) ";
	        PreparedStatement stmt = null;
	        ResultSet result = null;
	       

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, user);
	            stmt.setString(2, pass);

	            result = stmt.executeQuery();

	            result.next();
	            res= new Integer(result.getString("OIDUser"));
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }	
	            } catch (SQLException e) {
	            }
	        }

	        return res;

	}

	
	public boolean singOut() {
		
		return true ;
	}

	
	public Usuario obtenerUsuario(Integer oid) {
		
		String sql = "SELECT * FROM user WHERE (OIDUser = ? ) ";
        PreparedStatement stmt = null;
        ResultSet result = null;
        Usuario user = new Usuario();

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, oid);

            result = stmt.executeQuery();
            result.next();
            user.setNombre(result.getString("Nombre"));
            user.setApellidos(result.getString("Apellidos"));
            user.setProvincia(result.getString("Provincia")); 
            user.setArtistica(new Integer(result.getString("Artistica")));
            user.setCultural(new Integer(result.getString("Cultural")));
            user.setSindical(new Integer(result.getString("Sindical")));
            user.setSocial(new Integer(result.getString("Sociales")));
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }

        return user;
    }
	
	public void insertarUsuario(String n, String ap, String u, String con,String p,int a,int c,int sin,int soc,int mov){
		String sql = "INSERT INTO user(Nombre,Apellidos,nick,pass,Provincia,Cultural,Artistica,Sindical,Sociales,Movimiento15M)values(?,?,?,?,?,?,?,?,?,?)";
		
        PreparedStatement stmt = null;
       
        try {
        	  	
            stmt = conn.prepareStatement(sql);
            
            stmt.setString(1, n);
            stmt.setString(2, ap);
            stmt.setString(3, u);
            stmt.setString(4, con);
            stmt.setString(5, p);
            stmt.setInt(6, c);
            stmt.setInt(7, a);
            stmt.setInt(8, sin);
            stmt.setInt(9, soc);
            stmt.setInt(10, mov);
            stmt.executeUpdate();
            	                                
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
         } catch (SQLException e) {
            }
        }
		
	}
	

	
	
}
